#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    this->setTouchEnabled(true);

	m_testMove = true;
    visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCSize s = CCDirector::sharedDirector()->getWinSize();

	m_background = CCSprite::create("game_bg_2_hd.jpg");
	m_background->setAnchorPoint(ccp(0,0));
	this->addChild(m_background);

	
    // add "HelloWorld" splash screen"
	m_fish = CCSprite::create("fish1_01.png");

    // position the sprite on the center of the screen
    m_fish->setPosition(ccp(100,visibleSize.height/2));

    // add the sprite as a child to this layer
	this->addChild(m_fish);

	//CCLog("khoang cach di chuyen: %f", visibleSize.height/2);
	m_p = new vector<CCPoint>();
	m_p->push_back  (CCPoint(m_fish->getPositionX() , m_fish->getPositionY()));
	m_p->push_back  (CCPoint(m_fish->getPositionX() + 200, visibleSize.height/2 + 300));
	m_p->push_back (CCPoint(m_fish->getPositionX() + 400, visibleSize.height/2 - 300));
	m_p->push_back (CCPoint(m_fish->getPositionX() + 600,visibleSize.height/2 + 500));

	m_benzier = new Benzier(m_p, this);

	//use for ccbezierTo 
	this->schedule( schedule_selector(HelloWorld::update) ); 
    return true;

}

void HelloWorld::update(float dt)
{
	
	m_benzier->update(dt);
}


void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
    // Choose one of the touches to work with
    CCTouch* touch = (CCTouch*)( touches->anyObject() );
	CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);

    // Set up initial location of projectile
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	if (m_benzier->m_freeze == false)
	{
	    
		m_benzier->m_freeze  = true;
	}
	else 
	{
	
		m_benzier->m_freeze  = false;
	
	}
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
